package com.zcg.blogger.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zcg.blogger.dao.ArticleDao;
import com.zcg.blogger.dao.CategoryDao;
import com.zcg.blogger.dao.CommentDao;
import com.zcg.blogger.model.Article;
import com.zcg.blogger.model.ArticleLable;
import com.zcg.blogger.model.Category;
import com.zcg.blogger.model.CategoryArticle;
import com.zcg.blogger.model.Comment;
import com.zcg.blogger.model.Lable;
import com.zcg.blogger.pojo.Pager;
import com.zcg.blogger.service.ArticleService;

@Service("articleService")
public class ArticleServiceImpl implements ArticleService{

	@Resource	
	private ArticleDao articleDao;
	
	@Resource
	private CommentDao commentDao;
	
	@Resource
	private CategoryDao categoryDao;
	
	public void add(Article article) {
		article.setTime(new Date());
		this.articleDao.insert(article);
		List<CategoryArticle> categoryArticles = new ArrayList<CategoryArticle>();
		//循环添加到文章类别列表
		for(Category category:article.getCategorys()){
			//如果id==0则跳过
			if(category.getId() == 0){
				continue;
			}
			CategoryArticle categoryArticle = new CategoryArticle();
			categoryArticle.setArticle(article);
			categoryArticle.setCategory(category);
			categoryArticles.add(categoryArticle);
		}
		List<ArticleLable> articleLables = new ArrayList<ArticleLable>();
		//循环添加到文章标签列表
		for(Lable lable:article.getLables()){
			//如果id==0则跳过
			if(lable.getId() == 0 ){
				continue;
			}
			ArticleLable articleLable  = new ArticleLable();
			articleLable.setArticle(article);
			articleLable.setLable(lable);
			articleLables.add(articleLable);
		}
		this.articleDao.insertCategorys(categoryArticles);
		this.articleDao.insertLables(articleLables);
	}

	public void update(Article article) {
		this.articleDao.update(article);
	}

	public void delete(Article article) {
		int i = this.articleDao.delete(article.getId());
	}

	public Article loadById(int id) {
		return this.articleDao.findArticleById(id);
	}

	public List<Article> loadArticleAll() {
		Map map = new HashMap();
		map.put("pageOffset", Pager.getPager().getPageOffset());
		map.put("pageSize", Pager.getPager().getPageSize());
		return this.articleDao.selectAll(map);
	}

	public List<Category> loadArticleCategorys() {
		return this.categoryDao.selectFive();
	}

	public List<Article> loadArticlesByCategoryId(int id) {
		Map map = new HashMap();
		map.put("id", id);
		map.put("pageOffset", Pager.getPager().getPageOffset());
		map.put("pageSize", Pager.getPager().getPageSize());
		System.out.println(Pager.getPager());
		return this.categoryDao.selectArticleByCategoryId(map);
	}

	public Category loadByCategoryId(int categoryId) {
		return this.categoryDao.findBycategoryId(categoryId);
	}

	public Article loadByTitle(String title) {
		return null;
	}
	
	public List<Article> loadArticleHot(){
		return articleDao.selectHot();
	}
	
	public List<Comment> loadCommentByArticleId(int id){
		Map map = new HashMap();
		map.put("id", id);
		map.put("pageOffset", Pager.getPager().getPageOffset());
		map.put("pageSize", Pager.getPager().getPageSize());
		return commentDao.selectCommentByArticleId(map);
	}
	
	public int loadCommentCountByArticleId(int id){
		return commentDao.countByArticle(id);
	
	}
	
	public int addComment(Comment comment) {
		comment.setTime(new Date());
		return commentDao.insert(comment);
	}

	public void addPraise(int id){
		articleDao.addPraise(id);
	}

	public void addCommentPraise(int id){
		commentDao.addCommentPraise(id);
	}

	@Override
	public Article loadNextById(int id) {
		return this.articleDao.findNextById(id);
	}

	@Override
	public Article loadPreviousById(int id) {
		return this.articleDao.findPreviousById(id);
	}
	
}
